﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess;
using HTTT.Utilities;
using log4net;
using HTTT.DTO;

namespace HTTT.Bussiness
{
    public enum USERACTION
    {
        ADD =1
        , EDIT
        , DELETE
        , RESETPWD
    }



    public class HT_UserBO
    {
        public IEnumerable<HT_User> GetAllHT_User()
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.GetAllHT_User();
            
        }
        public HT_User GetHT_UserById(Guid id)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.GetHT_UserByID(id);
        }
        public bool UpdateHT_User(HT_User inHT_User)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.UpdateHT_User(inHT_User);
        }
        public ReturnValue<bool> InsertHT_User(HT_User inHT_User)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.InsertHT_User(inHT_User);
        }

        public ReturnValue<HT_User> InsertHT_UserAndReturn(HT_User inHT_User)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.InsertHT_UserAndReturn(inHT_User);
        }

        public ReturnValue<bool> DelHT_User(Guid id)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.DelHT_User(id);
        }

        internal ReturnValue<bool> DelHT_User(string username)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.DelHT_User(username);
        }


        public ReturnValue<bool> SynNewData(List<HT_User> listdata, string ouPath)
        {
            bool result = true;
            if (listdata == null)
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
            if (listdata.Count < 1)
            {
                return new ReturnValue<bool>(false, "Không có dữ liệu!");
            }
            HT_UserDAO _dao = new HT_UserDAO();
            _dao.DelAllHT_UserInOu(ouPath);
            foreach (HT_User item in listdata)
            {
                if (IsExist(item.sAMAccountName))
                {
                    DeleteHT_UserFromDB(item.sAMAccountName);
                }
                result = result && _dao.InsertHT_User(item).RetValue;
            }
            if (result)
            {
                return new ReturnValue<bool>(result, "Đồng bộ thành công " + listdata.Count + " bản ghi!");
            }
            else
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
        }

        private ReturnValue<bool> DeleteHT_UserFromDB(string username)
        {
            HT_UserDAO DAO = new HT_UserDAO();
            return DAO.DelHT_User(username);
        }

        private bool IsExist(string username)
        {
            HT_UserDAO DAO = new HT_UserDAO();
            return DAO.IsExist(username);
        }

        public IEnumerable<HT_User> SearchUser(string keyword, string ouPath)
        {
            HT_UserDAO _dao = new HT_UserDAO();
            return _dao.SearchUser(keyword, ouPath);
        }

        public bool DeleteUser(string username, string requestedBy, string requestInfo)
        {
            //Get this user by ID
            HT_UserDAO dao = new HT_UserDAO();
            HT_User user = dao.GetHT_UserByUsername(username);
            //Create reset pwd request
            HT_RequestDAO RequestDAO = new HT_RequestDAO();
            HT_Request request = new HT_Request();
            request.ID = Guid.NewGuid();
            request.Action = HT_ACTION.DELETE_USER.ToString();
            request.ActionName = "Xóa tài khoản <strong>" + user.sAMAccountName + "</strong>";
            request.ActionDescription = user.sAMAccountName; //only username of this account
            request.Note = requestInfo;
            request.Requestedby = requestedBy;
            request.RequestDate = DateTime.Now;
            request.Status = HT_REQUESTSTATUS.PEDDING.ToString();
            return RequestDAO.InsertRequest(request);
        }

        public bool ResetPwdUser(string username, string requestedBy, string requestInfo)
        {
            //Get this user by ID
            HT_UserDAO dao = new HT_UserDAO();
            HT_User user = dao.GetHT_UserByUsername(username);
            //Create reset pwd request
            HT_RequestDAO RequestDAO = new HT_RequestDAO();
            HT_Request request = new HT_Request();
            request.ID = Guid.NewGuid();
            request.Action = HT_ACTION.RESET_USER.ToString();
            request.ActionName = "Đặt lại mật khẩu tài khoản <strong>" + user.sAMAccountName + "</strong>";
            request.ActionDescription = user.sAMAccountName; //only username of this account
            request.Note = requestInfo;
            request.Requestedby = requestedBy;
            request.RequestDate = DateTime.Now;
            request.Status = HT_REQUESTSTATUS.PEDDING.ToString();
            return RequestDAO.InsertRequest(request);
        }

        public HT_User BuildHTUserFromAddUserData(string data)
        {
            HT_User user = new HT_User();
            string[] UserArr = data.Split(Constants.DATA_CUT);

            user.givenName = UserArr[0];
            user.initials = UserArr[1];
            user.sn = UserArr[2];
            user.displayName = UserArr[3];
            user.description = UserArr[4];
            user.mail = UserArr[5];
            user.wWWHomePage = UserArr[6];
            user.title = UserArr[7];
            user.physicalDeliveryOfficeName = UserArr[8];
            user.company = UserArr[9];
            user.department = UserArr[10];
            user.manager = UserArr[11];
            user.streetAddress = UserArr[12];
            user.postOfficeBox = UserArr[13];
            user.l = UserArr[14];
            user.postalCode = UserArr[15];
            user.st = UserArr[16];
            user.c = UserArr[17];
            user.homephone = UserArr[18];
            user.voiceTelephoneNumber = UserArr[19];
            user.mobile = UserArr[20];
            user.facsimileTelephoneNumber = UserArr[21];
            user.ipPhone = UserArr[22];
            user.info = UserArr[23];
            user.sAMAccountName = UserArr[24];
            user.userPrincipalName = UserArr[25];
            user.distinguishedName = UserArr[26];
            user.userAccountControl = Convert.ToInt64(UserArr[27]);
            if(UserArr[28] != "") user.pwdLastSet= Convert.ToDateTime(UserArr[28]);
            if (UserArr[29] != "") user.accountExpired = Convert.ToDateTime(UserArr[29]);
            if (UserArr[30] != "") user.whenCreated = Convert.ToDateTime(UserArr[30]);
            if (UserArr[31] != "") user.whenChanged = Convert.ToDateTime(UserArr[31]);
            return user;
        }

        public string BuildAddUserString(
            string givenName
            , string initials
            , string sn
            , string displayName
            , string description
            , string mail
            , string wWWHomePage
            , string title
            , string physicalDeliveryOfficeName
            , string company
            , string department
            , string manager
            , string streetAddress
            , string postOfficeBox
            , string l
            , string postalCode
            , string st
            , string c
            , string homephone
            , string voiceTelephoneNumber
            , string mobile
            , string facsimileTelephoneNumber
            , string ipPhone
            , string info
            , string sAMAccountName
            , string userPrincipalName
            , string distinguishedName
            , long userAccountControl
            , Nullable<DateTime> pwdLastSet
            , Nullable<DateTime> accountExpired
            , Nullable<DateTime> whenCreated
            , Nullable<DateTime> whenChanged)
        {
            // Append new infomation of user
            StringBuilder sb = new StringBuilder();
            sb.Append(givenName); sb.Append(Constants.DATA_CUT);
            sb.Append(initials); sb.Append(Constants.DATA_CUT);
            sb.Append(sn); sb.Append(Constants.DATA_CUT);
            sb.Append(displayName); sb.Append(Constants.DATA_CUT);
            sb.Append(description); sb.Append(Constants.DATA_CUT);
            sb.Append(mail); sb.Append(Constants.DATA_CUT);
            sb.Append(wWWHomePage); sb.Append(Constants.DATA_CUT);
            sb.Append(title); sb.Append(Constants.DATA_CUT);
            sb.Append(physicalDeliveryOfficeName); sb.Append(Constants.DATA_CUT);
            sb.Append(company); sb.Append(Constants.DATA_CUT);
            sb.Append(department); sb.Append(Constants.DATA_CUT);
            sb.Append(manager); sb.Append(Constants.DATA_CUT);
            sb.Append(streetAddress); sb.Append(Constants.DATA_CUT);
            sb.Append(postOfficeBox); sb.Append(Constants.DATA_CUT);
            sb.Append(l); sb.Append(Constants.DATA_CUT);
            sb.Append(postalCode); sb.Append(Constants.DATA_CUT);
            sb.Append(st); sb.Append(Constants.DATA_CUT);
            sb.Append(c); sb.Append(Constants.DATA_CUT);
            sb.Append(homephone); sb.Append(Constants.DATA_CUT);
            sb.Append(voiceTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(mobile); sb.Append(Constants.DATA_CUT);
            sb.Append(facsimileTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(ipPhone); sb.Append(Constants.DATA_CUT);
            sb.Append(info); sb.Append(Constants.DATA_CUT);
            sb.Append(sAMAccountName); sb.Append(Constants.DATA_CUT);
            sb.Append(userPrincipalName); sb.Append(Constants.DATA_CUT);
            sb.Append(distinguishedName); sb.Append(Constants.DATA_CUT);
            sb.Append(userAccountControl); sb.Append(Constants.DATA_CUT);
            sb.Append(pwdLastSet); sb.Append(Constants.DATA_CUT);
            sb.Append(accountExpired); sb.Append(Constants.DATA_CUT);
            sb.Append(whenCreated); sb.Append(Constants.DATA_CUT);
            sb.Append(whenChanged); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public HT_User GetHT_UserByUsername(string username)
        {
            HT_UserDAO UserDAO = new HT_UserDAO();
            return UserDAO.GetHT_UserByUsername(username);
        }


        public string BuildEditUserString(
            string givenName
            , string initials
            , string sn
            , string displayName
            , string description
            , string mail
            , string wWWHomePage
            , string title
            , string physicalDeliveryOfficeName
            , string company
            , string department
            , string manager
            , string streetAddress
            , string postOfficeBox
            , string l
            , string postalCode
            , string st
            , string c
            , string homephone
            , string voiceTelephoneNumber
            , string mobile
            , string facsimileTelephoneNumber
            , string ipPhone
            , string info
            , string sAMAccountName
            , string userPrincipalName
            , string distinguishedName
            , long userAccountControl
            , Nullable<DateTime> pwdLastSet
            , Nullable<DateTime> accountExpired
            , Nullable<DateTime> whenCreated
            , Nullable<DateTime> whenChanged)
        {
            StringBuilder sb = new StringBuilder();
            // Append new user infomation
            sb.Append(givenName); sb.Append(Constants.DATA_CUT);
            sb.Append(initials); sb.Append(Constants.DATA_CUT);
            sb.Append(sn); sb.Append(Constants.DATA_CUT);
            sb.Append(displayName); sb.Append(Constants.DATA_CUT);
            sb.Append(description); sb.Append(Constants.DATA_CUT);
            sb.Append(mail); sb.Append(Constants.DATA_CUT);
            sb.Append(wWWHomePage); sb.Append(Constants.DATA_CUT);
            sb.Append(title); sb.Append(Constants.DATA_CUT);
            sb.Append(physicalDeliveryOfficeName); sb.Append(Constants.DATA_CUT);
            sb.Append(company); sb.Append(Constants.DATA_CUT);
            sb.Append(department); sb.Append(Constants.DATA_CUT);
            sb.Append(manager); sb.Append(Constants.DATA_CUT);
            sb.Append(streetAddress); sb.Append(Constants.DATA_CUT);
            sb.Append(postOfficeBox); sb.Append(Constants.DATA_CUT);
            sb.Append(l); sb.Append(Constants.DATA_CUT);
            sb.Append(postalCode); sb.Append(Constants.DATA_CUT);
            sb.Append(st); sb.Append(Constants.DATA_CUT);
            sb.Append(c); sb.Append(Constants.DATA_CUT);
            sb.Append(homephone); sb.Append(Constants.DATA_CUT);
            sb.Append(voiceTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(mobile); sb.Append(Constants.DATA_CUT);
            sb.Append(facsimileTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(ipPhone); sb.Append(Constants.DATA_CUT);
            sb.Append(info); sb.Append(Constants.DATA_CUT);
            sb.Append(sAMAccountName); sb.Append(Constants.DATA_CUT);
            sb.Append(userPrincipalName); sb.Append(Constants.DATA_CUT);
            sb.Append(distinguishedName); sb.Append(Constants.DATA_CUT);
            sb.Append(userAccountControl); sb.Append(Constants.DATA_CUT);
            sb.Append(pwdLastSet); sb.Append(Constants.DATA_CUT);
            sb.Append(accountExpired); sb.Append(Constants.DATA_CUT);
            sb.Append(whenCreated); sb.Append(Constants.DATA_CUT);
            sb.Append(whenChanged); sb.Append(Constants.DATA_CUT);

            //Append old infomation of user
            HT_User OldUser = GetHT_UserByUsername(sAMAccountName);

            sb.Append(OldUser.givenName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.initials); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.sn); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.displayName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.description); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.mail); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.wWWHomePage); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.title); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.physicalDeliveryOfficeName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.company); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.department); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.manager); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.streetAddress); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.postOfficeBox); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.l); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.postalCode); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.st); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.c); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.homephone); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.voiceTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.mobile); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.facsimileTelephoneNumber); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.ipPhone); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.info); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.sAMAccountName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.userPrincipalName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.distinguishedName); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.userAccountControl); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.pwdLastSet); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.accountExpired); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.whenCreated); sb.Append(Constants.DATA_CUT);
            sb.Append(OldUser.whenChanged); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public HT_User GetNewHTUserFromEditUserData(string data)
        {
            HT_User user = new HT_User();
            string[] UserArr = data.Split(Constants.DATA_CUT);

            user.givenName = UserArr[0];
            user.initials = UserArr[1];
            user.sn = UserArr[2];
            user.displayName = UserArr[3];
            user.description = UserArr[4];
            user.mail = UserArr[5];
            user.wWWHomePage = UserArr[6];
            user.title = UserArr[7];
            user.physicalDeliveryOfficeName = UserArr[8];
            user.company = UserArr[9];
            user.department = UserArr[10];
            user.manager = UserArr[11];
            user.streetAddress = UserArr[12];
            user.postOfficeBox = UserArr[13];
            user.l = UserArr[14];
            user.postalCode = UserArr[15];
            user.st = UserArr[16];
            user.c = UserArr[17];
            user.homephone = UserArr[18];
            user.voiceTelephoneNumber = UserArr[19];
            user.mobile = UserArr[20];
            user.facsimileTelephoneNumber = UserArr[21];
            user.ipPhone = UserArr[22];
            user.info = UserArr[23];
            user.sAMAccountName = UserArr[24];
            user.userPrincipalName = UserArr[25];
            user.distinguishedName = UserArr[26];
            user.userAccountControl = Convert.ToInt64(UserArr[27]);
            if (UserArr[28] != "") user.pwdLastSet = Convert.ToDateTime(UserArr[28]);
            if (UserArr[29] != "") user.accountExpired = Convert.ToDateTime(UserArr[29]);
            if (UserArr[30] != "") user.whenCreated = Convert.ToDateTime(UserArr[30]);
            if (UserArr[31] != "") user.whenChanged = Convert.ToDateTime(UserArr[31]);
            return user;
        }
        public HT_User GetOldHTUserFromEditUserData(string data)
        {
            HT_User user = new HT_User();
            string[] UserArr = data.Split(Constants.DATA_CUT);

            user.givenName = UserArr[32];
            user.initials = UserArr[33];
            user.sn = UserArr[34];
            user.displayName = UserArr[35];
            user.description = UserArr[36];
            user.mail = UserArr[37];
            user.wWWHomePage = UserArr[38];
            user.title = UserArr[39];
            user.physicalDeliveryOfficeName = UserArr[40];
            user.company = UserArr[41];
            user.department = UserArr[42];
            user.manager = UserArr[43];
            user.streetAddress = UserArr[44];
            user.postOfficeBox = UserArr[45];
            user.l = UserArr[46];
            user.postalCode = UserArr[47];
            user.st = UserArr[48];
            user.c = UserArr[49];

            user.homephone = UserArr[50];
            user.voiceTelephoneNumber = UserArr[51];
            user.mobile = UserArr[52];
            user.facsimileTelephoneNumber = UserArr[53];
            user.ipPhone = UserArr[54];
            user.info = UserArr[55];
            user.sAMAccountName = UserArr[56];
            user.userPrincipalName = UserArr[57];
            user.distinguishedName = UserArr[58];
            user.userAccountControl = Convert.ToInt64(UserArr[59]);
            if (UserArr[60] != "") user.pwdLastSet = Convert.ToDateTime(UserArr[60]);
            if (UserArr[61] != "") user.accountExpired = Convert.ToDateTime(UserArr[61]);
            if (UserArr[62] != "") user.whenCreated = Convert.ToDateTime(UserArr[62]);
            if (UserArr[63] != "") user.whenChanged = Convert.ToDateTime(UserArr[63]);
            return user;
        }

        public string BuildBlockRequestData(string username, bool status)
        {
            StringBuilder sb = new StringBuilder();
            // Append new user infomation
            sb.Append(username); sb.Append(Constants.DATA_CUT);
            sb.Append(status); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public string GetUsernameFromBlockRequest(string data)
        {
            string[] DataArr = data.Split(Constants.DATA_CUT);
            return DataArr[0];
        }

        public bool GetStatusFromBlockRequest(string data)
        {
            string[] DataArr = data.Split(Constants.DATA_CUT);
            return Convert.ToBoolean(DataArr[1]);
        }

        public bool BlockUser(string username, string requestedBy, string requestInfo)
        {
            //Get this user by ID
            HT_UserDAO dao = new HT_UserDAO();
            HT_User user = dao.GetHT_UserByUsername(username);
            //Create reset pwd request
            HT_RequestDAO RequestDAO = new HT_RequestDAO();
            HT_Request request = new HT_Request();
            request.ID = Guid.NewGuid();
            request.Action = HT_ACTION.BLOCK_USER.ToString();
            request.ActionName = "Khóa tài khoản <strong>" + user.sAMAccountName + "</strong>";
            request.ActionDescription = BuildBlockRequestData(user.sAMAccountName, false);
            request.Note = requestInfo;
            request.Requestedby = requestedBy;
            request.RequestDate = DateTime.Now;
            request.Status = HT_REQUESTSTATUS.PEDDING.ToString();
            return RequestDAO.InsertRequest(request);
        }

        public bool UnBlockUser(string username, string requestedBy, string requestInfo)
        {
            //Get this user by ID
            HT_UserDAO dao = new HT_UserDAO();
            HT_User user = dao.GetHT_UserByUsername(username);
            //Create reset pwd request
            HT_RequestDAO RequestDAO = new HT_RequestDAO();
            HT_Request request = new HT_Request();
            request.ID = Guid.NewGuid();
            request.Action = HT_ACTION.UN_BLOCK_USER.ToString();
            request.ActionName = "Mở khóa tài khoản <strong>" + user.sAMAccountName + "</strong>";
            request.ActionDescription = BuildBlockRequestData(user.sAMAccountName, true);
            request.Note = requestInfo;
            request.Requestedby = requestedBy;
            request.RequestDate = DateTime.Now;
            request.Status = HT_REQUESTSTATUS.PEDDING.ToString();
            return RequestDAO.InsertRequest(request);
        }

    }
}
